package com.xqx.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author：xingquanxiang createTime：2019/10/4 23:52
 * description:
 */
public class MyInterceptor implements HandlerInterceptor {
    /**
     * 在目标方法执行之前 执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle.........");
        String param = request.getParameter("param");
        if ("yes".equals(param)) {
            return true;
        }else{
            request.getRequestDispatcher("error.jsp").forward(request,response);
            return false;
        }
    }

    /**
     * 在目标方法执行之后 视图对象返回之前 执行
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        modelAndView.addObject("username", "xqx");
        System.out.println("postHandle..........");
    }

    /**
     * 在流程都执行完毕后 执行
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion.........");
    }
}
